package com.xysd.examproject.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xysd.examproject.R;

import java.util.List;

/**
 * Created by donghui on 2017/6/12.
 */

public class WifiListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    List<ScanResult> list;

    public WifiListAdapter(Context context, List<ScanResult> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ScanResult getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({ "ViewHolder", "InflateParams" })
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int level=0;
        ViewHolder holder=null;
        if (convertView==null){
            convertView=inflater.inflate(R.layout.wifi_listitem,null);
            holder=new ViewHolder();
            holder.wifi_ssid= (TextView) convertView.findViewById(R.id.ssid);
            holder.wifi_level= (ImageView) convertView.findViewById(R.id.wifi_level);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        ScanResult scanResult = list.get(position);
        holder.wifi_ssid.setText(scanResult.SSID);
        Log.i("scanResult.SSID", "scanResult.SSID="+scanResult);
        level= WifiManager.calculateSignalLevel(scanResult.level,5);
        if(scanResult.capabilities.contains("WEP")||scanResult.capabilities.contains("PSK")||
                scanResult.capabilities.contains("EAP")){
            holder.wifi_level.setImageResource(R.drawable.wifi_signal_lock);
        }else{
            holder.wifi_level.setImageResource(R.drawable.wifi_signal_open);
        }
        holder.wifi_level.setImageLevel(level);
        //判断信号强度，显示对应的指示图标  

        return convertView;
    }

    class ViewHolder{

        TextView wifi_ssid;
        ImageView wifi_level;
    }
}
